<?php
/**
 * 用户表模型
 * User: 王文凡<787802868@qq.com>
 * Date: 2016/7/28 0028
 * Time: 14:05
 */
namespace Common\Model;

use Think\Model;

class MemberModel extends Model
{
    protected $_validate = [
        ['username', 'require', '用户名必须填写！'],
        ['password', 'require', '密码必须填写！'],
        ['password', '6,18', '密码长度错误!', 0, 'length'],
        ['username', '', '用户名已经存在！', 0, 'unique', 1],
        ['repassword', 'password', '确认密码不正确', 0, 'confirm'],
        ['group_id', 'require', '用户组必须选择！'],
        ['email', 'email', '邮箱格式错误！'],
        ['phone_number', 'require', '手机号必须填写！'],
        ['phone_number', '', '手机号已经存在！', 0, 'unique', 1],
        ['phone_number', '/^1[3,5,7,8,9][0-9]{9}$/', '手机格式错误！',0,'regex'],

    ];

    //新增用户
    public function adduser($id = '')
    {
        if (!$this->create()) {
            $arr['state'] = 'error';
            $arr['info'] = $this->getError();
            return $arr;
        } else {
            $data['username'] = $_POST['username'];
            $data['password'] = md5($_POST['password']);
            $data['nickname'] = $_POST['username'];
            $data['email'] = $_POST['email'];
            $data['phone_number']=$_POST['phone_number'];
            if (empty($id)) {
                $this->startTrans();
                if ($uid = $this->add($data)) {
                    $vo['uid'] = $uid;
                    $vo['group_id'] = $_POST['group_id'];
                    if (M('AuthGroupAccess')->add($vo)) {
                        $this->commit();
                        $arr['state'] = 'success';
                        $arr['info'] = '新增成功';
                    } else {
                        $this->rollback();
                        $arr['state'] = 'error';
                        $arr['info'] = '新增失败1';
                    }
                } else {
                    $this->rollback();
                    $arr['state'] = 'error';
                    $arr['info'] = '新增失败';
                }
            }
            return $arr;

        }
    }

    //查询用户信息
    public function lister($id = '')
    {
        if (empty($id)) {
            $counts = $this->count();
            $page = page($counts, 20, 5);
            $list = $this->alias("a")->field("a.*,c.title,b.group_id")->join("sr_auth_group_access as b on a.id=b.uid")
                ->join("sr_auth_group as c on b.group_id=c.id")
                ->order("a.id desc")->limit($page->firstRow, $page->listRows)->select();
            $arr['list'] = $list;
            $arr['page'] = $page;
            return $arr;
        } else {
            return $this->where("id=%d", $id)->find();
        }
    }

    //详细用户信息
    public function userDetail($uid)
    {
        $info = $this->alias("a")->field("a.*,c.title,b.group_id,d.area_name")
            ->join("sr_auth_group_access as b on a.id=b.uid")
            ->join("sr_auth_group as c on b.group_id=c.id")
            ->join("sr_area as d on c.area_id=d.id")
            ->where("a.id=%d",$uid)->find();
        return $info;
    }


    //用户登录
    public function doLogin()
    {

        $password = md5($_POST['password']);
        $username = $_POST['username'];
        $result = $this->where("username='%s' and password='%s'", [$username, $password])->find();
        if ($result) {
            if ($result['status'] == 1) {
                $uid = $result['id'];
                $datas = M('AuthGroupAccess')->field("b.area_id,a.group_id")->alias("a")
                    ->join("sr_auth_group as b on a.group_id=b.id")
                    ->where("a.uid=$uid")
                    ->find();
                $datas['uid'] = $uid;
                $datas['nickname']=$result['nickname'];
                session('tc', $datas);
                $iipp=$_SERVER["REMOTE_ADDR"];
                $info=['end_time'=>time(),'end_ip'=>$iipp];
                $this->where("id=1")->save($info);
                $arr['state'] = 'success';
                $arr['info'] = '登录成功';

            } else {
                $arr['state'] = 'error';
                $arr['info'] = '账号已禁用';
            }
        } else {
            $arr['state'] = 'error';
            $arr['info'] = '账号或密码错误';
        }
        return $arr;

    }
    //修改密码
    public function changePw(){
        $password=$_POST['password'];
        $newpwd=$_POST['newpwd'];
        $renewpwd=$_POST['renewpwd'];
        $uid=$_SESSION['tc']['uid'];
        if($newpwd==$renewpwd && !empty($newpwd)){
           $pwre= $this->where("id=%d and password='%s'",array($uid,md5($password)))->find();
            if($pwre){
              if($this->where("id=%d",$uid)->save(['password'=>md5($newpwd)]) ){
                  $arr['state']='success';
                  $arr['info']='更改密码成功';
              } else{
                  $arr['state']='error';
                  $arr['info']='更改密码失败';
              }
            }else{
                $arr['state']='error';
                $arr['info']='原密码错误';
            }
            return $arr;

        }else{
            $arr['state']='error';
            $arr['info']='两次密码不一样';
            return $arr;
        }
    }
}

















